/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SubMenuItem.java
 *
 * Created on Mar 8, 2011, 3:06:26 PM
 */

package imat;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 *
 * @author pierrereimertz
 */
public class SubMenuItem extends ImageButton implements MouseListener{

    String name;
    Color gron = new Color(0x169721);
    int type = 1;

    /** Creates new form SubMenuItem */
    public SubMenuItem(String name) {

        super();
        this.name = name;
        setText(name);
        setImageBackground("dot.png");
        addMouseListener(this);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 245, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 89, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void setType(int type) {
        this.type = type;
    }

    public void mouseClicked(MouseEvent me) {
    }

    public void mousePressed(MouseEvent me) {
    }

    public void mouseReleased(MouseEvent me) {
        if (isButtonClicked(me)) {
            if (type == 1) {
                DataHandler.getInstance().getMainWindow().getProductList().setCategory(CategoryName.valueOf(getActionCommand()));
                DataHandler.getInstance().mark = name;
            }
            else {
                DataHandler.getInstance().getMainWindow().leftPanelButtonPressed(name);
                return;
            }
            setBackground(gron);
                text.setForeground(Color.BLACK);
                repaint();

                SubMenuItem lastClick = DataHandler.getInstance().getLastClicked();
                if(lastClick != null && lastClick != this){
                    DataHandler.getInstance().getLastClicked().setOpaque(false);
                    DataHandler.getInstance().getLastClicked().text.setForeground(Color.BLACK);
                    lastClick.repaint();
                }
                
                repaint();
                DataHandler.getInstance().setLastClicked(this);
        }
    }

    public void mouseEntered(MouseEvent me) {
               
                setBackground(gron);
                text.setForeground(Color.WHITE);
                setOpaque(true);
                repaint();
    }

    public void mouseExited(MouseEvent me) {
        SubMenuItem lastClick = DataHandler.getInstance().getLastClicked();
        if(lastClick != this){
            setOpaque(false);
            
        }
        text.setForeground(Color.BLACK);
    repaint();
    }

    public void setMarked() {
        setBackground(gron);
        text.setForeground(Color.WHITE);
        setOpaque(true);
        repaint();
        DataHandler.getInstance().setLastClicked(this);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
